# Plugin configuration
PLUGIN_ID := example
PLUGIN_NAME := Example Plugin
PLUGIN_DESCRIPTION := An example plugin
PLUGIN_AUTHOR := Your Name
PLUGIN_VERSION := v0.0.1
PLUGIN_LOADER := native_plugin_http

# Server configuration
SERVER_URL := http://localhost:8080/api/v1
API_ENDPOINT := $(SERVER_URL)/plugin

# Build configuration
BUILD_DIR := build
SOURCE_DIR := .
PLUGIN_DIR := src
BUILD_FILE := $(BUILD_DIR)/plugin.so

# Time-based variables
NOW_TIME := $(shell date +%Y%m%d%H%M%S)
NEW_MODULE_NAME := "plugin$(NOW_TIME)"

define PLUGIN_META
{
  "id": "$(PLUGIN_ID)",
  "name": "$(PLUGIN_NAME)",
  "description": "$(PLUGIN_DESCRIPTION)",
  "author": "$(PLUGIN_AUTHOR)",
  "version": "$(PLUGIN_VERSION)",
  "loader": "$(PLUGIN_LOADER)"
}
endef

FORMAT_META := $(shell echo '$(PLUGIN_META)' | tr -d '\n' | tr -s ' ' | sed 's/"/\\"/g')

# Curl options
CURL_OPTS := --insecure -s
CURL_POST_OPTS := $(CURL_OPTS) -X POST

# Common curl command templates
define CURL_GET
curl $(CURL_OPTS) "$(API_ENDPOINT)/$(1)$(2)" | jq
endef

define CURL_POST
curl $(CURL_POST_OPTS) "$(API_ENDPOINT)/$(1)$(2)" | jq
endef

define CURL_UPLOAD
curl $(CURL_POST_OPTS) \
    -F "file=@$(BUILD_FILE)" \
    -F "meta=$(FORMAT_META)" \
    "$(API_ENDPOINT)/$(1)" | jq
endef

.PHONY: build clean init list run load unload check-file help show-meta

# Default target - build and deploy
run: build init

# Build the plugin
build: clean
	@echo "Building plugin: $(PLUGIN_ID) ($(PLUGIN_VERSION))..."
	mkdir -p $(BUILD_DIR)
	cp -r $(PLUGIN_DIR)/* $(BUILD_DIR)/
	rm -rf $(BUILD_DIR)/go.mod
	touch $(BUILD_DIR)/go.mod
	echo "module plugin${NOW_TIME}" > $(BUILD_DIR)/go.mod
	echo "\ngo 1.23.10" >> $(BUILD_DIR)/go.mod
	find ./$(BUILD_DIR) -type f \( -name "*.go" -o -name "go.mod" \) | while read file; do \
		sed -i "s/plugin[0-9]\{14\}/${NEW_MODULE_NAME}/g" $$file; \
	done
	cd $(BUILD_DIR) && GO111MODULE=on CGO_ENABLED=1 go build -ldflags="-s -w" -buildmode=plugin -o plugin.so .
	@echo "Plugin built successfully: $(BUILD_DIR)"

# Clean build directory
clean:
	@echo "Cleaning build directory..."
	rm -rf $(BUILD_DIR)

# Initialize/upload a plugin (alias for upload)
init: build
	@echo "Uploading plugin: $(PLUGIN_ID) ($(PLUGIN_VERSION))..."
	@$(call CURL_UPLOAD,init)

# Load a plugin but not run it
load: build
	@echo "Loading plugin: $(PLUGIN_ID) ($(PLUGIN_VERSION))..."
	@$(call CURL_UPLOAD,load)

# List all plugins
list:
	@echo "Listing all plugins..."
	@$(call CURL_GET,list)

# Execute a plugin
exec:
	@echo "Executing plugin: $(PLUGIN_ID)..."
	@$(call CURL_GET,run,?plugin_id=$(PLUGIN_ID))

# Unload a plugin
unload:
	@echo "Unloading plugin: $(PLUGIN_ID)..."
	@$(call CURL_POST,unload,?plugin_id=$(PLUGIN_ID))

# Show plugin metadata
show-meta:
	@echo "Plugin configuration:"
	@echo "  ID:          $(PLUGIN_ID)"
	@echo "  Name:        $(PLUGIN_NAME)"
	@echo "  Version:     $(PLUGIN_VERSION)"
	@echo "  Author:      $(PLUGIN_AUTHOR)"
	@echo "  Loader:      $(PLUGIN_LOADER)"
	@echo "  Source:      $(PLUGIN_FILE)"
	@echo "  Build file:  $(BUILD_DIR)"
	@echo ""
	@echo "Formatted JSON metadata:"
	@echo '$(PLUGIN_META)' | jq '.'
	@echo ""
	@echo "Single-line escaped metadata:"
	@echo "$(FORMAT_META)"

# Check if source file exists
check-file:
	@test -f $(PLUGIN_FILE) || (echo "Error: $(PLUGIN_FILE) not found" && exit 1)

# Display help information
help:
	@echo "Available targets:"
	@echo "  run        - Default target (build and init)"
	@echo "  build      - Build plugin to build directory"
	@echo "  clean      - Clean build directory"
	@echo "  init       - Initialize/upload plugin (alias for upload)"
	@echo "  load       - Load plugin to server"
	@echo "  list       - List all plugins on server"
	@echo "  exec       - Execute/run plugin"
	@echo "  unload     - Unload plugin from server"
	@echo "  show-meta  - Show plugin metadata and configuration"
	@echo "  check-file - Check if source file exists"
	@echo "  help       - Show this help message"
	@echo ""
	@echo "Configuration:"
	@echo "  Plugin ID:      $(PLUGIN_ID)"
	@echo "  Plugin Name:    $(PLUGIN_NAME)"
	@echo "  Plugin File:    $(PLUGIN_FILE)"
	@echo "  Build Version:  $(PLUGIN_VERSION)"
	@echo "  Server URL:     $(SERVER_URL)"
	@echo ""
	@echo "Usage examples:"
	@echo "  make run                    # Build and upload"
	@echo "  make build                  # Build only"
	@echo "  make upload PLUGIN_ID=test # Upload with custom ID"
	@echo "  make exec                   # Execute plugin"
	@echo "  make show-meta              # Show configuration"
	@echo "  make clean                  # Clean build files"

# Override variables from command line or environment
# Example: make run PLUGIN_ID=test PLUGIN_VERSION=v1.0.0 SERVER_URL=https://prod.example.com